﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Trading
{
    public partial class ePlaceOrder : WeifenLuo.WinFormsUI.Docking.DockContent  
    {
        public ePlaceOrder()
        {
            InitializeComponent();            
        }

        private void ePlaceOrder_Load(object sender, EventArgs e)
        {
            try
            {
                
                ABSCommonLibrary.SetSymbols(ref cboSymbol);
                ABSCommonLibrary.SetComboBoxControl(ref cboSide,
                    new MyComboItem[2]{new MyComboItem("2", "MUA"),
                        new MyComboItem("1", "BÁN")});                
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString()); 
            }
        }

        private void cboSymbol_Validated(object sender, EventArgs e)
        {
            DataRow[] rows = DeclareLibrary.SYMBOLS.Tables[0]
               .Select("CODE = '" + cboSymbol.Text.ToUpper() + "'");
            if (rows.Length > 0)
            {
                lblCePrice.Text =Convert.ToDecimal( rows[0]["upper_price"]).ToString(DeclareLibrary.CURRENCY_FORMAT);
                lblFlPrice.Text = Convert.ToDecimal(rows[0]["lower_price"]).ToString(DeclareLibrary.CURRENCY_FORMAT);
                
            }
            else
            {
                
                lblCePrice.ResetText();
                lblFlPrice.ResetText();
            }
        }
        private void CalcOrderValue()
        {
            decimal quote_price = 0;
            if (cboPrice.Text == "ATO" || cboPrice.Text == "ATC")
            {
                DataRow[] rows = DeclareLibrary.SYMBOLS.Tables[0]
                    .Select("CODE = '" + cboSymbol.Text + "'");
                if (rows.Length > 0)
                {
                    if (((MyComboItem)cboSide.SelectedItem).ID == "1")
                        quote_price = Convert.ToDecimal(rows[0]["lower_price"]);
                    else
                        quote_price = Convert.ToDecimal(rows[0]["lower_price"]);
                }
                else
                    quote_price = 0;
            }
            else
            {
                if (!decimal.TryParse(cboPrice.Text, out quote_price))
                {
                    quote_price = 0;
                }
            }
            lblAmount.Text = Convert.ToDecimal(nbrVolume.Value * quote_price)
                .ToString("###,###,###,###");
        }

        private void nbrVolume_Validated(object sender, EventArgs e)
        {
            CalcOrderValue();
        }

        private void cboPrice_Validated(object sender, EventArgs e)
        {
            if (cboPrice.Text.Trim().ToUpper() == "ATO"
                || cboPrice.Text.Trim().ToUpper() == "ATC")
                cboPrice.Text = cboPrice.Text.Trim().ToUpper();
            else
            {
                decimal quote_price = 0;
                if (!decimal.TryParse(cboPrice.Text, out quote_price))
                    cboPrice.Text = string.Empty;
                //else
                //    cboPrice.Text = quote_price.ToString();
            }
            CalcOrderValue();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtAccountNo.ResetText();
            cboSymbol.ResetText();
            cboPrice.Text = string.Empty;
            nbrVolume.Value = 0;
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                
                string sOrderText = cboSide.Text.ToLower() + " " +
                    nbrVolume.Value.ToString(DeclareLibrary.CURRENCY_FORMAT) + " " +
                    cboSymbol.Text + " giá " + cboPrice.Text +               
                    "tài khoản " + txtAccountNo.Text;

                if (ABSCommonLibrary.ShowQuestion(
                    "Bạn muốn đặt lệnh " + sOrderText + " ?") == DialogResult.Yes)
                {
                    decimal quote_price = 0;
                    if (cboPrice.Text.Trim().ToUpper() == "ATO"
                        || cboPrice.Text.Trim().ToUpper() == "ATC")
                        cboPrice.Text = cboPrice.Text.Trim().ToUpper();
                    else
                        if (!decimal.TryParse(cboPrice.Text, out quote_price))
                        {
                            ABSCommonLibrary.ShowMessage("Gia giao dich khong hop le");
                            return;
                        }
                    object[] p_param = new object[8];
                    if (((MyComboItem)cboSide.SelectedItem).ID == "1")
                        p_param[0] = "S";
                    else
                        p_param[0] = "B";
                    
                    p_param[1] = cboSymbol.Text;
                    p_param[2] = nbrVolume.Value;
                    p_param[3] = cboPrice.Text.Trim().ToUpper();
                    p_param[4] = txtAccountNo.Value;                                        
                    p_param[5] = DeclareLibrary.MARKET_DAY;
                    p_param[6] = DeclareLibrary.USER_NAME;
                    int error_code = Convert.ToInt32(ABSCommonLibrary.DoExecute(
                        CommonLibrary.CommonLibrary.BOSC002021, ref p_param));
                    switch (error_code)
                    {
                        case -1:
                            ABSCommonLibrary.ShowMessage("Tai khoan khach hang khong ton tai");
                            txtAccountNo.Focus();
                            break;
                        case -2:
                            ABSCommonLibrary.ShowMessage("Chung khoan lua chon khong ton tai");
                            cboSymbol.Focus();
                            break;
                        case -3:
                            ABSCommonLibrary.ShowMessage("Gia dat phai nam giua gia tran va gia san");
                            cboPrice.Focus();
                            break;
                        case -4:
                            ABSCommonLibrary.ShowMessage("Khoi luong dat phai chan No 10 chung khoan tai HOSE");
                            nbrVolume.Focus();
                            break;
                        case -13:
                            ABSCommonLibrary.ShowMessage("Khoi luong dat phai thap hon 20,000 chung khoan doi voi lenh thuong tai HOSE");
                            nbrVolume.Focus();
                            break;
                        case -5:
                            ABSCommonLibrary.ShowMessage("Gia phai chan 100 VND voi gia dat nho hon 50,000 tai HOSE");
                            cboPrice.Focus();
                            break;
                        case -6:
                            ABSCommonLibrary.ShowMessage("Gia phai chan 500 VND voi muc gia nho hon 100,000 lon hon 50,000");
                            cboPrice.Focus();
                            break;
                        case -7:
                            ABSCommonLibrary.ShowMessage("Gia phai chan 1000 VND voi muc gia lon hon 100,000 tai HOSE");
                            cboPrice.Focus();
                            break;
                        case -8:
                            ABSCommonLibrary.ShowMessage("Khong dat lenh ATO, ATC tren tai HASTC");
                            cboPrice.Focus();
                            break;
                        case -9:
                            ABSCommonLibrary.ShowMessage("Khoi luong dat phai chan No 100 chung khoan tai HASTC");
                            cboPrice.Focus();
                            break;
                        case -14:
                            ABSCommonLibrary.ShowMessage("Gia dat phai chan 100 VND tai san HASTC");
                            cboPrice.Focus();
                            break;
                        case -10:
                            ABSCommonLibrary.ShowMessage("Khong du chung khoan dat ban");
                            cboSymbol.Focus();
                            break;
                        case -11:
                            ABSCommonLibrary.ShowMessage("Khong du tien dat lenh mua");
                            cboSymbol.Focus();
                            break;
                        case -12:
                            ABSCommonLibrary.ShowMessage("Khong dat lenh mua ban nguoc chieu");
                            cboSide.Focus();
                            break;

                        case -15:
                            ABSCommonLibrary.ShowMessage("Tong khoi luong mua trong ngay khong duoc phep");
                            nbrVolume.Focus();
                            break;
                        case -20:
                            ABSCommonLibrary.ShowMessage("CP/TP/CCQ dang tam ngung giao dich");
                            cboSymbol.Focus();
                            break;
                        case -21:
                            ABSCommonLibrary.ShowMessage("Lenh mua nha dau tu nuoc ngoai khong vuot qua room quy dinh");
                            txtAccountNo.Focus();
                            break;
                        case -22:
                            ABSCommonLibrary.ShowMessage("Giao dich thoa thuan, quang cao phai dat khoi luong >= 20,000 va nho hon 84,000,000 CP");
                            nbrVolume.Focus();
                            break;
                        case -23:
                            ABSCommonLibrary.ShowMessage("Giao dich khong duoc phep dat gia ATO,ATC");
                            cboPrice.Focus();
                            break;
                        case -24:
                            ABSCommonLibrary.ShowMessage("Phai giao dich thoa thuan, quang cao doi voi trai phieu");
                            cboSymbol.Focus();
                            break;
                        case -25:
                            ABSCommonLibrary.ShowMessage("Gia giao dich trai phieu phai nam trong khoan tran san");
                            cboPrice.Focus();
                            break;
                        case -30:
                            ABSCommonLibrary.ShowMessage("Khong du so du tien mat trong tai khoan de dat lenh nay");
                            nbrVolume.Focus();
                            break;
                        case -31:
                            ABSCommonLibrary.ShowMessage("Khong du so du chung khoan trong tai khoan de dat lenh nay");
                            nbrVolume.Focus();
                            break;
                        default:
                            ABSCommonLibrary.ShowMessage("Lenh dat " + sOrderText + " thanh cong");
                            break;
                    }                    

                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
        
        private void cboSide_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((MyComboItem)cboSide.SelectedItem).ID == "1")
                panel1.BackColor = Color.Red;
            else
                panel1.BackColor = Color.Blue;
        }

        private void txtAccountNo_Validated(object sender, EventArgs e)
        {
            try
            {
                DataSet dtsCashInfo = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC004007, new object[1] { txtAccountNo.Text });
                if (dtsCashInfo.Tables[0].Rows.Count > 0)
                {
                    DataRow _row = dtsCashInfo.Tables[0].Rows[0];
                    toolStripStatusCashAmount.Text =
                        Convert.ToDecimal(_row["trading_amount"])
                        .ToString(DeclareLibrary.CURRENCY_FORMAT);
                    toolStripStatusAssetsAmount.Text =
                        Convert.ToDecimal(_row["begin_amount"])
                        .ToString(DeclareLibrary.CURRENCY_FORMAT);
                    toolStripStatusDebitAmount.Text =
                        Convert.ToDecimal(_row["advance_amount"])
                        .ToString(DeclareLibrary.CURRENCY_FORMAT);
                    toolStripStatusTradingAmount.Text =
                       Convert.ToDecimal(Convert.ToDecimal(_row["trading_amount"]) + Convert.ToDecimal(_row["begin_amount"])
                         - Convert.ToDecimal(_row["advance_amount"])).ToString(DeclareLibrary.CURRENCY_FORMAT);
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString()); ;
            }
        }

    }
}